package com.yunkaweilai.android.activity.login;

import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.TextView;

import com.google.gson.Gson;
import com.yunkaweilai.android.R;
import com.yunkaweilai.android.base.BaseActivity;
import com.yunkaweilai.android.constants.Api;
import com.yunkaweilai.android.model.login.ForgetFirstStpModel;
import com.yunkaweilai.android.net.HttpRequest;
import com.yunkaweilai.android.net.PostProcess;
import com.yunkaweilai.android.utils.CountDownTimerUtils;
import com.yunkaweilai.android.utils.TitleBuilder;
import com.yunkaweilai.android.utils.Utils;
import com.yunkaweilai.android.view.dialog.login.ForgetCodeDialogNew;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;

/**
 * 忘记密码第二次提交
 * Created by Administrator on 2017/7/22.
 */

public class ForgetPassStp2Activity extends BaseActivity {


    @BindView(R.id.id_tv_time)
    TextView idTvTime;
    @BindView(R.id.id_btn_next)
    Button idBtnNext;
    @BindView(R.id.tv_phone_number)
    TextView tvPhone;
    @BindView(R.id.id_edt_code)
    TextView idEdtCode;
    public static final String USER_NAME = "USER_NAME";
    public static final String PHONE_NUMBER = "PHONE_NUMBER";
    public static final String FORGET_OR_REGISTER = "FORGET_OR_REGISTER";
    public static final String FORGET_PASSWORD_TOKEN = "FORGET_PASSWORD_TOKEN";
    public static final String PASSWORD = "PASSWORD";
    public static final String INDUSTRYCODE = "INDUSTRYCODE";
    private int ForgetOrRegister = 0;//注册还是找回密码 1为找回  0为注册
    private String UserName = "";
    private String PhoneNumber = "";
    private String Password = "";
    private String IndustryCode = "";
    private CountDownTimerUtils countDownTimerUtils;

    // 这里进来是忘记密码
    public static void actionStart(Context context, String UserName, String PhoneNumber, int forgetOrRegister) {
        Intent intent = new Intent(context, ForgetPassStp2Activity.class);
        intent.putExtra(USER_NAME, UserName);
        intent.putExtra(PHONE_NUMBER, PhoneNumber);
        intent.putExtra(FORGET_OR_REGISTER, forgetOrRegister);
        context.startActivity(intent);
    }

    // 这里进来是注册
    public static void actionStart(Context context, String UserName, String PhoneNumber, String Password, String IndustryCode) {
        Intent intent = new Intent(context, ForgetPassStp2Activity.class);
        intent.putExtra(USER_NAME, UserName);
        intent.putExtra(PHONE_NUMBER, PhoneNumber);
        intent.putExtra(PASSWORD, Password);
        intent.putExtra(INDUSTRYCODE, IndustryCode);
        context.startActivity(intent);
    }


    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_forger_pass_stp2);
        ButterKnife.bind(this);
        PhoneNumber = getIntent().getStringExtra(PHONE_NUMBER);
        UserName = getIntent().getStringExtra(USER_NAME);
        ForgetOrRegister = getIntent().getIntExtra(FORGET_OR_REGISTER, 0);
        switch (ForgetOrRegister) {
            case 1:
                break;
            case 0:
                Password = getIntent().getStringExtra(PASSWORD);
                IndustryCode = getIntent().getStringExtra(INDUSTRYCODE);
                break;
        }
        new TitleBuilder(mAc).setTitleText("安全验证")
                .setLeftImage(R.mipmap.ic_go_back)
                .setLeftOnClickListener(new View.OnClickListener() {
                    @Override
                    public void onClick(View v) {
                        finish();
                    }
                });
        tvPhone.setText(PhoneNumber.substring(0, 3) + "****" + PhoneNumber.substring(PhoneNumber.length() - 4));
        countDownTimerUtils = new CountDownTimerUtils(idTvTime, 60000, 1000);
    }

    //验证参数是否为空
    private void isRight() {
        switch (ForgetOrRegister){
            case 1:
                initHttp();
                break;
            case 0:
                initHttp1();
                break;
        }

    }

    private void initHttp() {//忘记密码网络请求
        HttpRequest.postUrl(Api.FORGET_PASSWORD_STP1)
                .addParams("phone", PhoneNumber)
                .addParams("UserName", UserName)
                .addParams("PhoneVerifyCode", idEdtCode.getText().toString())
                .execute(new PostProcess.StringCallBack() {
                             @Override
                             public void onResponse(String json) {
                                 boolean checkCode = Utils.checkCode(mAc, json);
                                 if (checkCode) {
                                     Gson gson = new Gson();
                                     ForgetFirstStpModel forgetFirstStpModel = gson.fromJson(json, ForgetFirstStpModel.class);
                                     ForgetPassStp3Activity.actionStart(mAc, forgetFirstStpModel.getData().getForget_password_token());
//                                     finish();
                                 }
                                 showLog(json);
                             }

                             @Override
                             public void onError(Exception e) {

                             }
                         }
                );
    }

    private void initHttp1() {//注册网络请求
        HttpRequest.postUrl(Api.REGISTER_URL)
                .addParams("phone", PhoneNumber)
                .addParams("UserName", UserName)
                .addParams("Password", Password)
                .addParams("PhoneVerifyCode", idEdtCode.getText().toString())
                .addParams("industry_code", IndustryCode)
                .addParams("is_agreement", "1")
                .execute(new PostProcess.StringCallBack() {
                    @Override
                    public void onResponse(String json) {
                        boolean checkCode = Utils.checkCode(mAc, json);
                        if (checkCode) {
                            ForgetPassStp3Activity.actionStart(mAc, true);
//                            finish();
                        }
                    }

                    @Override
                    public void onError(Exception e) {
                        showToast(R.string.http_error);
                    }
                });
    }

    @OnClick({R.id.id_tv_time, R.id.id_btn_next})
    public void onViewClicked(View view) {
        switch (view.getId()) {
            case R.id.id_tv_time://获取验证码
                if (ForgetOrRegister == 1)
                {
                    ForgetCodeDialogNew dialog = new ForgetCodeDialogNew(mAc, UserName, PhoneNumber, countDownTimerUtils, "1");
                    dialog.show();
                } else {
                    ForgetCodeDialogNew dialog = new ForgetCodeDialogNew(mAc, UserName, PhoneNumber, countDownTimerUtils, "0");
                    dialog.show();
                }
                break;
            case R.id.id_btn_next://下一步
                isRight();
                break;
        }
    }
}
